﻿using System;
using Blockey.ScreenManagers;
using BlockeyEssentials;
using BlockeyEssentials.Drawing;
using BlockeyEssentials.Input;
using BlockeyEssentials.ScreenSystem;
using BlockeyEssentials.ScreenSystem.ScreenTypes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Blockey.MenuSystemScreens
{
    public class SearchResultsScreen : Screen
    {
        public SearchResultsScreen(ScreenManager oManager)
            : base(oManager)
        {
            _TextFont = new Font(All.Font_Debug);
            _TextFont.Color = Color.Black;
            _TextFont.Layer = 95;
        }

        #region Methods

        public override void Update()
        {
            if (Active)
            {
                Input();
            }
        }

        public override void Draw(SpriteBatch oSB)
        {

        }

        private void Select()
        {
            if (Networking.SelectedSession != null)
            {
                var oScreen = new AttemptJoinScreen(_Manager, this);
                _Manager.AddScreen(oScreen);
            }
        }

        private void Cancel()
        {
            var oScreen = new XboxLiveMenuScreen(_Manager);
            GoTo(oScreen);
        }

        #region Helper Methods

        private void Input()
        {
            var oUp = false;
            var oDown = false;
            var oLeft = false;
            var oRight = false;
            var oSelect = false;
            var oCancel = false;

            var oKeyboardInput = InputManager.Keyboard;
            if (oKeyboardInput.JustPressed(Keys.Up))
                oUp = true;
            if (oKeyboardInput.JustPressed(Keys.Down))
                oDown = true;
            if (oKeyboardInput.JustPressed(Keys.Left))
                oLeft = true;
            if (oKeyboardInput.JustPressed(Keys.Right))
                oRight = true;
            if (oKeyboardInput.JustPressed(Keys.Enter, Keys.F))
                oSelect = true;
            if (oKeyboardInput.JustPressed(Keys.D))
                oCancel = true;

            var oGamePadInput = InputManager.GamePad1;
            if (oGamePadInput.JustPressed(Buttons.DPadUp))
                oUp = true;
            if (oGamePadInput.JustPressed(Buttons.DPadDown))
                oDown = true;
            if (oGamePadInput.JustPressed(Buttons.DPadLeft))
                oLeft = true;
            if (oGamePadInput.JustPressed(Buttons.DPadRight))
                oRight = true;
            if (oGamePadInput.JustPressed(Buttons.A))
                oSelect = true;
            if (oGamePadInput.JustPressed(Buttons.B))
                oCancel = true;

            if (oUp)
            {
                return;
            }

            if (oDown)
            {
                return;
            }

            if (oLeft)
            {
                return;
            }
            if (oRight)
            {
                return;
            }

            if (oSelect)
            {
                Select();
                return;
            }

            if (oCancel)
            {
                Cancel();
                return;
            }

        }

        #endregion

        #region IAsyncResult Methods

        private void GoToMatch(IAsyncResult oResult)
        {
            Remove();
            _DestinationManager = new GameplayManager3D(_Manager.Game);
        }

        #endregion

        #endregion

        private Font _TextFont;

    }
}
